//api.js

//导入axios
import axios from 'axios'
import * as admin from './admin'
import * as classify from './classify'
import * as tag from './tag'
import * as user from './user'
import * as category from './category'
import * as post from './post'
import * as comment from './comment'
import * as favorite from './favorite'
import * as likes from './likes'
import * as dashboard from './dashboard'
import * as ai from './ai'
//使用axios下面的create([config])方法创建axios实例，其中config参数为axios最基本的配置信息。
const API = axios.create({
  baseURL: 'http://'+import.meta.env.VITE_APP_ADRESS+':'+import.meta.env.VITE_APP_PORT, //请求后端数据的基本地址，自定义
  timeout: 2000,                   //请求超时设置，单位ms
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
API.interceptors.request.use(
  config => {
    // 在发送请求之前做些什么
    let token = sessionStorage.getItem('token');
    let authentication = sessionStorage.getItem('authentication');

    if (token) {
      config.headers.token = token;
    }
    if (authentication) {
      config.headers.authentication = authentication;
    }
    return config;
  },
  error => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 响应拦截器
API.interceptors.response.use(
  response => {
    // 对响应数据做点什么
    return response;
  },
  error => {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

//导出我们建立的axios实例模块，ES6 export用法
export default API;

export { admin, classify, tag, user, category, post, comment, favorite, likes, dashboard ,ai}